/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pig.pigpen;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.hadoop.mapred.JobConf;
import org.apache.pig.pigpen.preferences.PreferenceConstants;
import org.eclipse.jface.preference.IPreferenceStore;
import org.osgi.framework.Bundle;

public class PluginConfiguration {

    private static ConsoleLogger logger = ConsoleLogger.getDefault();

    // map = "pair01;pair02;pair03"
    private final static String PAIR_SEPARATOR = ";";

    // pair = "key=>value"
    private final static String KEY_VALUE_SEPARATOR = "=>";

    public static String getJarPath() {
        final IPreferenceStore store = PigPenActivator.getDefault()
                .getPreferenceStore();
        final String path = store.getString(PreferenceConstants.P_PATH);
        return path;
    }

    public static String createList(Map<String, String> items) {
        final StringBuffer result = new StringBuffer();
        final Set<String> keys = items.keySet();
        for (String key : keys) {
            result.append(key);
            result.append(KEY_VALUE_SEPARATOR);
            result.append(items.get(key));
            result.append(PAIR_SEPARATOR);
        }
        return result.toString();
    }

    public static Map<String, String> parseString(String stringList) {
        final String[] pairs = stringList.split(PAIR_SEPARATOR);
        final Map<String, String> map = new HashMap<String, String>();
        for (String pair : pairs) {
            if (pair.contains(KEY_VALUE_SEPARATOR)) {
                final String[] tmp = pair.split(KEY_VALUE_SEPARATOR);
                map.put(tmp[0], tmp[1]);
            }
        }
        return map;
    }

    public static String findContainingJar(Class clazz) {
        final String path = getJarPath();
        if (!"".equals(path)) {
            logger.debug("Using jar file set in preferences: " + path);
            return path;
        } else {
            final Bundle bundle = PigPenActivator.getDefault().getBundle();
            final String location = bundle.getLocation().replace("update@", "");
            logger.debug("location: " + location);
            final File jar = new File(location + "lib/pig.jar");
            if (jar.exists()) {
                final String bundlePath = jar.getAbsolutePath();
                logger
                        .debug("No user specify jar file, using pluging bundle pig jar: "
                                + bundlePath);
                return bundlePath;
            }
            return null;
        }
    }
}
